package com.dev.shiro.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import java.util.List;
import java.util.Arrays;
import java.util.Objects;
import java.util.ArrayList;
import com.dev.shiro.service.UserOnlineService;
import com.dev.shiro.entity.UserOnline;


/**
 *
 *   前端控制器
 *
 * @author: admin
 * @date: 2022-03-17
 */
@RestController
@RequestMapping({"userOnline"})
@Api(tags = "管理")
public class UserOnlineController {

    @Autowired
    private UserOnlineService service;

    @ApiOperation(value = "列表查询")
    @GetMapping({"list"})
    public ResponseEntity<List<UserOnline>> list(UserOnline entity){
        QueryWrapper<UserOnline> qw = new QueryWrapper<UserOnline>();
        qw.setEntity(entity).orderByAsc("session_id");
        return ResponseEntity.ok(service.list(qw));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping({"page"})
    public ResponseEntity<Page> page(UserOnline entity,Page page){
        QueryWrapper<UserOnline> qw = new QueryWrapper<UserOnline>();
        qw.setEntity(entity).orderByAsc("session_id");
        return ResponseEntity.ok(service.page(page,qw));
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping({"/getOne/{id}"})
    public ResponseEntity getById(@PathVariable("id") int id) {
        return ResponseEntity.ok(service.getById(id));
    }

    @ApiOperation(value = "新增")
    @PostMapping({"save"})
    public ResponseEntity save(@RequestBody UserOnline entity) {
        return ResponseEntity.ok(service.save(entity));
    }

    @ApiOperation(value = "批量新增")
    @PostMapping({"saveBatch"})
    public ResponseEntity saveBatch(@RequestBody List<UserOnline> entitys) {
        if(CollectionUtils.isEmpty(entitys)){
            return ResponseEntity.ok("参数为空!");
        }
        return ResponseEntity.ok(service.saveBatch(entitys));
    }

    @ApiOperation(value = "更新")
    @PutMapping({"update"})
    public ResponseEntity update(@RequestBody UserOnline entity) {
        if(Objects.isNull(entity.getSessionId())){
            return ResponseEntity.ok("参数为空!");
        }
        var result = service.getById(entity.getSessionId());
        if(Objects.nonNull(result)){
            return ResponseEntity.ok(service.updateById(entity));
        }else{
            return ResponseEntity.ok("数据不存在!");
        }
    }

    @ApiOperation(value = "批量更新")
    @PutMapping({"updateBatch"})
    public ResponseEntity updateBatch(@RequestBody List<UserOnline> entitys) {
        if(CollectionUtils.isEmpty(entitys)){
            return ResponseEntity.ok("参数为空!");
        }
        return ResponseEntity.ok(service.updateBatchById(entitys));
    }

    @ApiOperation(value = "根据主键删除")
    @DeleteMapping({"/remove/{id}"})
    public ResponseEntity remove(@PathVariable("id") int id) {
        var entity = service.getById(id);
        if(Objects.nonNull(entity)){
            return ResponseEntity.ok(service.removeById(id));
        }else{
            return ResponseEntity.ok("数据不存在!");
        }
    }

    @ApiOperation(value = "批量根据主键删除")
    @DeleteMapping({"/remove/{ids}"})
    public ResponseEntity removeAll(@PathVariable("ids") int[] ids) {
        if (Objects.isNull(ids) || ids.length < 1){
            return ResponseEntity.ok("参数为空!");
        }
        return ResponseEntity.ok(service.removeByIds(new ArrayList<>(Arrays.asList(ids))));
    }
}
